package com.lky.controller;

import com.lky.domain.NewUser;
import com.lky.domain.User;
import com.lky.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * 名称:UserController
 * 描述:
 *
 * @version 1.0
 * @author:likaiyang
 * @datetime:2023-11-14 16:07
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //查询全部用户
    @RequestMapping("/findAllUser")
    public String findAllUser(Model model){
        List<User> userList=userService.findAllUser();
        model.addAttribute("alluser",userList);
        return "/user/alluser";//跳转到alluser.jsp
    }

    //从系统首页跳转到添加用户页面
    @RequestMapping("/toinsertUser")
    public String toinsertUser(){
        return "/user/insertuser";
    }
    //添加用户，成功则再次执行查询全部用户
    @RequestMapping("/insertUser")
    public String insertUser(User user,Model model){
        try{
            int i=userService.insertUser(user);
            if(i>0){
                List<User> userList=userService.findAllUser();
                model.addAttribute("alluser",userList);
                model.addAttribute("msg","添加用户成功");
                return "/user/alluser";
            }
            else{
                model.addAttribute("msg","添加用户失败");
                return "/user/alluser";
            }
        }catch (Exception e){
            List<User> userList=userService.findAllUser();
            model.addAttribute("alluser",userList);
            model.addAttribute("msg","添加用户失败，可能原因为用户名重复！");
            return "/user/alluser";
        }
    }

    //根据id查询用户，用于更新和删除
    @RequestMapping("/findUserById")
    public User findUserById(@RequestParam(name = "id") Integer id){
        User user=userService.findUserById(id);
        return user;
    }

    //跳转到修改用户信息的页面
    @RequestMapping("toupdateUserById/{id}")
    public String toupdateUserById(@PathVariable(name = "id")Integer id,Model model){
        User user=userService.findUserById(id);
        model.addAttribute("user",user);
        return "/user/updateuser";
    }
    //根据id更新用户信息
    @RequestMapping("/updateUser")
    public String updateUser(User user,Model model){
        try{
            int i=userService.updateUserById(user);
            if(i>0){
                List<User> userList=userService.findAllUser();
                model.addAttribute("alluser",userList);
                model.addAttribute("msg","修改用户信息成功！");
                return "/user/alluser";
            }
            else{
                model.addAttribute("msg","修改失败！");
                return "/user/alluser";
            }
        }catch (Exception e){
            List<User> userList=userService.findAllUser();
            model.addAttribute("alluser",userList);
            model.addAttribute("msg","修改失败,可能原因为用户名重复！");
            return "/user/alluser";
        }
    }

    //根据id删除哟用户
    @RequestMapping("/deleteUserById/{id}")
    public String deleteUserById(@PathVariable(name = "id")Integer id,Model model){
        User user=userService.findUserById(id);
        System.out.println(user.toString());
        NewUser newUser=new NewUser();
        newUser.setT_user_id(user.getId());
        newUser.setUsername(user.getUsername());
        newUser.setPassword(user.getPassword());
        newUser.setPhone(user.getPhone());
        newUser.setEmail(user.getEmail());
        newUser.setAdmin(user.getAdmin());
        int j=userService.insertUserToNew_User(newUser);
        int i=userService.deleteUserById(id);

        if(i>0&&j>0){
            List<User> userList=userService.findAllUser();
            model.addAttribute("alluser",userList);
            model.addAttribute("msg","删除用户信息成功");
            return "/user/alluser";
        }
        else{
            List<User> userList=userService.findAllUser();
            model.addAttribute("alluser",userList);
            model.addAttribute("msg","修改失败");
            return "/user/alluser";
        }
    }

    //前往登录页面
    @RequestMapping("/tologin")
    public String tologin(){
        return "../login";
    }
    //登录操作
    @RequestMapping("login")
    public String Login(User user,Model model,HttpSession session){
        User user1=userService.findUserByNameAndPassword(user);
        if(user1!=null){
            if(Objects.equals(user1.getAdmin(), "true")){
                session.setAttribute("USER_SESSION",user1);
                System.out.println("管理员：{"+user1.getUsername()+"}登录成功");
                return "../index";
            }
            else{
                session.setAttribute("USER_SESSION",user1);
                System.out.println("普通用户：{"+user1.getUsername()+"}登录成功");
                return "user/index_new";
            }
        }
        else {
            model.addAttribute("msg","用户名或密码错误，请重新登录！");
            System.out.println("登录失败");
            return "../login";
        }
    }


    @RequestMapping("/toregister")
    public String toRegister(){
        return "../register";
    }
    //普通用户
    @RequestMapping("/toregister_new")
    public String toRegister_new(){
        return "/user/register_new";
    }


    @RequestMapping("/register")
    public String Register(User user,Model model){
        //User user1=userService.findUserByName(user);
//        if(userService.findUserByName(user)==null){
//            if(user.getUsername()!=null&&user.getPassword()!=null){
//                int i=userService.insertUser(user);
//                model.addAttribute("msg","注册成功！");
//                System.out.println("注册成功");
//                return "../login";
//            }
//            else{
//                model.addAttribute("msg","注册失败,请正确填写注册信息！");
//                System.out.println("注册失败");
//                return "../register_new";
//            }
//        }
//        else{
//            model.addAttribute("msg","当前用户名已被注册，请重新注册！");
//            System.out.println("用户名{"+user.getUsername()+"}已被注册");
//            return "../register_new";
//        }
        if(user.getUsername()!=null&&user.getPassword()!=null){
            if(userService.findUserByName(user)==null){
                int i=userService.insertUser(user);
                model.addAttribute("msg","注册成功！");
                System.out.println("注册成功");
                return "../login";
            }
            else{
                model.addAttribute("msg","当前用户名已被注册，请重新注册！");
                System.out.println("用户名{"+user.getUsername()+"}已被注册");
                return "../register_new";
            }
        }
        else{
            model.addAttribute("msg","注册失败,请正确填写注册信息！");
            System.out.println("注册失败");
            return "../register_new";
        }
    }

    //普通用户
    @RequestMapping("/register_new")
    public String Register_new(User user,Model model){
        User user1=userService.findUserByName(user);
        if(user1==null){
            if(user.getUsername()!=null&&user.getPassword()!=null){
                int i=userService.insertUser_new(user);
                return "../login";
            }
            else{
                model.addAttribute("msg","注册失败！");
                return "/user/register_new";
            }
        }
        else{
            model.addAttribute("msg","当前用户名已被注册，请重新注册！");
            return "/user/register_new";
        }
    }

    //用户退出
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/user/toIndex";//用户重新登录重新定向到系统首页
    }

    //跳转到首页
    @RequestMapping("/toIndex")
    public String toIndex(){
        return "../index";
    }
    //跳转到首页-普通用户
    @RequestMapping("/toIndex_new")
    public String toIndex_new(){
        return "/user/index_new";
    }

    //模糊查询
    @RequestMapping("/findUserByStr")
    public String findUserByStr(@RequestParam(value="searchStr") String searchStr,Model model){
        List<User> userList=userService.findUserByStr(searchStr);
        model.addAttribute("alluser",userList);
        return "/user/alluser";
    }
}
